package com.itheima.controller.backend.security;

import com.itheima.service.OrdersettingService;
import io.swagger.v3.oas.models.responses.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrdersettingController {
    @Autowired
    private OrdersettingService ordersettingService;

    @GetMapping("/download")
    public ResponseEntity<byte[]> download() throws IOException {
        // 调用 service 获取生成好的 Excel 字节数组
        byte[] excelBytes = ordersettingService.generateOrderSettingExcel();

        HttpHeaders headers = new HttpHeaders();
        String filename = "ordersetting_template.xlsx";
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", URLEncoder.encode(filename, "UTF-8"));

        return ResponseEntity.ok()
                .headers(headers)
                .body(excelBytes);
    }
    @PostMapping("/upload")
    public ResponseEntity<ApiResponse> upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            // 调用 service 处理文件上传
            ordersettingService.handleFileUpload(excelFile);
            return ResponseEntity.ok(new ApiResponse());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ApiResponse());
        }
    }
    @PostMapping("/editNumberByOrderDate")
    public ResponseEntity<ApiResponse> editNumberByOrderDate(@RequestBody Map<String, String> requestParams) {
        try {
            // 获取请求参数
            String number = requestParams.get("number");
            String orderDate = requestParams.get("orderDate");

            // 调用 service 处理业务逻辑
            ordersettingService.editNumberByOrderDate(number, orderDate);

            // 返回成功响应
            return ResponseEntity.ok(new ApiResponse());
        } catch (Exception e) {
            e.printStackTrace();
            // 返回错误响应
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ApiResponse());
        }
    }
    @GetMapping("/getOrderSettingByMonth")
    public ResponseEntity<ApiResponse> getOrderSettingByMonth(@RequestParam String month) {
        try {
            // 调用 service 获取预约设置数据
            ApiResponse response = ordersettingService.getOrderSettingByMonth(month);

            // 返回成功响应
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            // 返回错误响应
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ApiResponse());
        }
    }


}
